﻿/// <reference path="new_common_scripts.js" />
/// <reference path="new_XrmPage_vsdoc.js" />
/// <reference path="new_jquery.js" />
/// <reference path="new_json.js" />
/// <reference path="new_XrmServiceToolkit.js" />


var opportunity = {
    addOnChangeFunctions: function () {
        Xrm.Page.getAttribute("new_publishabook").addOnChange(opportunity.new_publishabook_onchange);
        Xrm.Page.getAttribute("new_publishbookfor").addOnChange(opportunity.new_publishbookfor_onchange);
        Xrm.Page.getAttribute("new_book_owner").addOnChange(opportunity.new_book_owner_onchange);
        Xrm.Page.getAttribute("new_youarewriting").addOnChange(opportunity.new_youarewriting_onchange);
        Xrm.Page.getAttribute("new_manuscript_ready").addOnChange(opportunity.new_manuscript_ready_onchange);
        Xrm.Page.getAttribute("new_pictures_exist").addOnChange(opportunity.new_pictures_exist_onchange);
        Xrm.Page.getAttribute("new_pictures_owner").addOnChange(opportunity.new_pictures_owner_onchange);
        Xrm.Page.getAttribute("new_need_delivery").addOnChange(opportunity.new_need_delivery_onchange);
        Xrm.Page.getAttribute("new_distribution_other").addOnChange(opportunity.new_distribution_other_onchange);
        Xrm.Page.getAttribute("new_need_translate").addOnChange(opportunity.new_need_translate_onchange);
    },

    onLoad: function (onload_flag) {
        var _text = "Приблизительный объем рукописи";
        $('<tr id="count_of_pages" height="30"><td colspan="2"><div>' + _text + '</div></td></tr>').
            insertBefore($("#new_count_of_pages_a4_c").parent().parent());

        _text = "Какими из нижеперечисленных услуг издательства Вы хотели бы воспользоваться";
        $('<tr id="services" height="30"><td colspan="2"><div>' + _text + '</div></td></tr>').
            insertBefore($("#new_service_correction_c").parent().parent());

        _text = "Где было бы Вам интересно продавать свою книгу?";
        $('<tr id="distribution" height="30"><td colspan="2"><div>' + _text + '</div></td></tr>').
            insertBefore($("#new_distributingbookin_bookshop_c").parent().parent());

        opportunity.addOnChangeFunctions();

        opportunity.new_publishabook_onchange();
        opportunity.new_publishbookfor_onchange();
        opportunity.new_book_owner_onchange();
        opportunity.new_youarewriting_onchange();
        opportunity.new_manuscript_ready_onchange();
        opportunity.new_pictures_exist_onchange();
        opportunity.new_pictures_owner_onchange();
        opportunity.new_need_delivery_onchange();
        opportunity.new_distribution_other_onchange();
        opportunity.new_need_translate_onchange();
    },

    new_publishabook_onchange: function (onload_flag) {
        var new_publishabook = common.getAttrValue("new_publishabook");

        if (new_publishabook == 100000001) {
            common.showControl("new_publishbookfor", true);
            common.showControl("new_publishbookfor_other", false);
            common.showSection("Summary_section_6");
        }
        else {
            common.hideControl("new_publishbookfor", false);
            common.hideControl("new_publishbookfor_other", false);
            common.hideSection("Summary_section_6", true);
        }
        opportunity.new_publishbookfor_onchange();

        if (new_publishabook == 100000002) {
            common.showControl("new_publishing_house", true);
            common.showControl("new_publishing_year", true);
            common.showControl("new_book_owner", true);
            common.showControl("new_owning_to_year", true);
        }
        else {
            common.hideControl("new_publishing_house", false);
            common.hideControl("new_publishing_year", false);
            common.hideControl("new_book_owner", false);
            common.hideControl("new_owning_to_year", false);
        }
        opportunity.new_book_owner_onchange();

        common.setDisabledControl("new_publishabook_other", (new_publishabook != 100000003));
        common.setRequiredControl("new_publishabook_other", (new_publishabook == 100000003));
        if (new_publishabook != 100000003)
            common.setAttrValue("new_publishabook_other", null);
    },

    new_publishbookfor_onchange: function () {
        var new_publishbookfor = common.getAttrValue("new_publishbookfor");

        common.setDisabledControl("new_publishbookfor_other", (new_publishbookfor != 100000003));
        common.setRequiredControl("new_publishbookfor_other", (new_publishbookfor == 100000003));
        if (new_publishbookfor != 100000003)
            common.setAttrValue("new_publishbookfor_other", null);
    },

    new_book_owner_onchange: function () {
        var new_book_owner = common.getAttrValue("new_book_owner");

        if (new_book_owner == 100000002) {
            common.showControl("new_new_owning_to_date", true);
        } else {
            common.hideControl("new_new_owning_to_date", false);
        }
        //       common.setDisabledControl("new_owning_to_year", (new_book_owner != 100000002));
        //       common.setRequiredControl("new_owning_to_year", (new_book_owner == 100000002));
        //        if (new_book_owner != 100000002)
        //           common.setAttrValue("new_owning_to_year", null);
    },

    new_youarewriting_onchange: function () {
        var new_youarewriting = common.getAttrValue("new_youarewriting");

        common.setDisabledControl("new_youarewriting_other", (new_youarewriting != 100000003));
        common.setRequiredControl("new_youarewriting_other", (new_youarewriting == 100000003));
        if (new_youarewriting != 100000003)
            common.setAttrValue("new_youarewriting_other", null);
    },

    new_manuscript_ready_onchange: function () {
        var new_manuscript_ready = common.getAttrValue("new_manuscript_ready");

        common.setDisabledControl("new_manuscript_ready_other", (new_manuscript_ready != 100000006));
        common.setRequiredControl("new_manuscript_ready_other", (new_manuscript_ready == 100000006));
        if (new_manuscript_ready != 100000006)
            common.setAttrValue("new_manuscript_ready_other", null);
    },

    new_pictures_exist_onchange: function () {
        var new_pictures_exist = common.getAttrValue("new_pictures_exist");

        if (new_pictures_exist == true) {
            common.showControl("new_count_of_pictures", true);
            common.showControl("new_colourfull_of_pictures", true);
            common.showControl("new_pictures_owner", true);
            common.showControl("new_pictures_owner_other", true);
            common.showControl("new_need_delivery", true);
            common.showControl("new_delivery_address", true);
        }
        else {
            common.hideControl("new_count_of_pictures", false);
            common.hideControl("new_colourfull_of_pictures", false);
            common.hideControl("new_pictures_owner", false);
            common.hideControl("new_pictures_owner_other", false);
            common.hideControl("new_need_delivery", false);
            common.hideControl("new_delivery_address", false);
        }
        opportunity.new_pictures_owner_onchange();
        opportunity.new_need_delivery_onchange();
    },

    new_pictures_owner_onchange: function () {
        var new_pictures_owner = common.getAttrValue("new_pictures_owner");

        common.setDisabledControl("new_pictures_owner_other", (new_pictures_owner != 100000003));
        common.setRequiredControl("new_pictures_owner_other", (new_pictures_owner == 100000003));
        if (new_pictures_owner != 100000003)
            common.setAttrValue("new_pictures_owner_other", null);
    },

    new_need_delivery_onchange: function () {
        var new_need_delivery = common.getAttrValue("new_need_delivery");

        common.setDisabledControl("new_delivery_address", (new_need_delivery != 100000002));
        common.setRequiredControl("new_delivery_address", (new_need_delivery == 100000002));
        if (new_need_delivery != 100000002)
            common.setAttrValue("new_delivery_address", null);
    },

    new_distribution_other_onchange: function () {
        var new_distribution_other = common.getAttrValue("new_distribution_other");

        common.setDisabledControl("new_distribution_other_text", (new_distribution_other != true));
        common.setRequiredControl("new_distribution_other_text", (new_distribution_other == true));
        if (new_distribution_other != true)
            common.setAttrValue("new_distribution_other_text", null);
    },

    new_need_translate_onchange: function () {
        var new_need_translate = common.getAttrValue("new_need_translate");

        common.setDisabledControl("new_need_translate_language", (new_need_translate != true));
        common.setRequiredControl("new_need_translate_language", (new_need_translate == true));
        if (new_need_translate != true)
            common.setAttrValue("new_need_translate_language", null);
    }
}